package com.example.a_java.service.impl;

import com.example.a_java.dto.ApprovalQuery;
import com.example.a_java.dto.PageResult;
import com.example.a_java.entity.Product;
import com.example.a_java.entity.ProductApproval;
import com.example.a_java.mapper.ProductApprovalFeatureMapper;
import com.example.a_java.mapper.ProductApprovalMapper;
import com.example.a_java.mapper.ProductMapper;
import com.example.a_java.service.ProductApprovalService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 产品审核服务实现
 */
@Service
@RequiredArgsConstructor
public class ProductApprovalServiceImpl implements ProductApprovalService {

    private final ProductApprovalMapper productApprovalMapper;
    private final ProductMapper productMapper;
    private final ProductApprovalFeatureMapper productApprovalFeatureMapper;

    @Override
    public PageResult<ProductApproval> getApprovalList(ApprovalQuery query) {
        // 查询审核总数
        Long total = productApprovalMapper.countApprovals(query);
        if (total == 0) {
            return PageResult.build(0L, List.of());
        }
        // 查询审核列表
        List<ProductApproval> approvals = productApprovalMapper.selectApprovalList(query);
        return PageResult.build(total, approvals);
    }

    @Override
    public ProductApproval getApprovalById(Long id) {
        return productApprovalMapper.selectApprovalById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approveProduct(ProductApproval approval) {
        // 校验审核状态
        ProductApproval existApproval = productApprovalMapper.selectApprovalById(approval.getId());
        if (existApproval == null) {
            throw new RuntimeException("审核记录不存在");
        }
        if (!"pending".equals(existApproval.getStatus())) {
            throw new RuntimeException("只能审核待审核状态的产品");
        }

        // 设置审核时间
        approval.setApprovalTime(LocalDateTime.now());
        // 更新审核状态
        productApprovalMapper.updateApproval(approval);

        // 如果审核通过，更新产品状态或创建产品
        if ("approved".equals(approval.getStatus())) {
            Product product = new Product();
            product.setId(existApproval.getProductId());
            product.setName(existApproval.getName());
            product.setType(existApproval.getType());
            product.setCategoryId(existApproval.getCategoryId());
            product.setCategoryName(existApproval.getCategoryName());
            product.setInterestRate(existApproval.getInterestRate());
            product.setLoanAmount(existApproval.getLoanAmount());
            product.setLoanTerm(existApproval.getLoanTerm());
            product.setRepaymentMethod(existApproval.getRepaymentMethod());
            product.setApplyCondition(existApproval.getApplyCondition());
            product.setRequiredMaterials(existApproval.getRequiredMaterials());
            product.setDescription(existApproval.getDescription());
            product.setFeatures(existApproval.getFeatures());
            product.setStatus("active");
            product.setUpdateTime(LocalDateTime.now());

            if (product.getId() == null) {
                // 创建新产品
                product.setCreateTime(LocalDateTime.now());
                productMapper.insertProduct(product);
            } else {
                // 更新产品
                productMapper.updateProduct(product);
            }
        }
    }
} 
